#include <bits/stdc++.h>
using namespace std;

struct node {
	int value;
	int dist;
	node* left;
	node* right;
};

node* merge(node* a, node* b) {
	if (a == NULL && b == NULL) return NULL;
	else if (a == NULL) return b;
	else if (b == NULL) return a;
	
	node* r = a->value < b->value ? a : b;
	node* c = a->value > b->value ? a : b;
	
	if (r->left == NULL) {
		r->left = c;
		r->dist = 0;
	}
	else if (r->left->dist < c->dist) {
		node* t = r->right;
		r->right = r->left;
		r->left = c;
		r->right = merge(r->right, t);
		r->dist = r->right->dist + 1;
	}
	else {
		r->right = c;
		r->dist = c->dist + 1;
	}
	
	return r;
}


int main() {
    int n;
    cin >> n;
    
    int v;
    queue<node*> q;
    
    for (int i=0; i<n; ++i) {
    	cin >> v;
    	struct node tn = {
			.value = v,
			.dist = 0,
			.left = NULL,
			.right = NULL,
		};
		q.push(&tn);
	}
	
	while(q.size() > 1) {
		node* a = q.front();
		q.pop();
		node* b = q.front();
		q.pop();
		node* t = merge(a, b);
		q.push(t);
	}
	
	node* ltree = q.front();
	q.pop();
	
	cout << "top = " << ltree->value << endl;
	
	while(cin >> v && v>0) {
		struct node a = {
			.value = v,
			.dist = 0,
			.left = NULL,
			.right = NULL,
		};
		ltree = merge(ltree, &a);
		cout << "top = " << ltree->value << endl;
	}
	
	cout << ltree->left->value << endl;
	
	return 0;
}

